package cn.onekit;

import cn.onekit.android.ACTIVITY;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class MESSAGE {

	static Map<String, BroadcastReceiver> broadcastReceivers = new HashMap<String, BroadcastReceiver>();
	static Map<String, List<CALLBACK<Bundle>>> globalCallbacks = new HashMap<String, List<CALLBACK<Bundle>>>();
	static Map<String, Map<String, List<CALLBACK<Bundle>>>> receiverCallbacks = new HashMap<String, Map<String, List<CALLBACK<Bundle>>>>();

	/**
	 * sendMessage 发送消息
	 * 
	 * @param message
	 *            :发送字段
	 */
	public static void send(String message, Object receiver, Bundle params) {
		if (!broadcastReceivers.containsKey(message)) {
			IntentFilter filter = new IntentFilter();
			if (receiver != null) {
				filter.addCategory(receiver.toString());
			}
			filter.addAction(message);
			BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {

					String message = intent.getAction();
					Set<String> categories = intent.getCategories();
					List<CALLBACK<Bundle>> callbacks;
					if (categories != null && categories.size() == 1) {
						String category = categories.toArray(new String[categories.size()])[0];
						callbacks = receiverCallbacks.get(message).get(category);
					} else {
						callbacks = globalCallbacks.get(message);
					}
					for (CALLBACK<Bundle> callback : callbacks) {
						callback.run(false, intent.getExtras());
					}
					BroadcastReceiver broadcastReceiver = broadcastReceivers.get(message);
					ACTIVITY.context.unregisterReceiver(broadcastReceiver);
					broadcastReceivers.remove(message);
				}

			};
			ACTIVITY.context.registerReceiver(broadcastReceiver, filter);
			broadcastReceivers.put(message, broadcastReceiver);
		}
		//
		Intent intent = new Intent();
		if (receiver != null) {
			intent.addCategory(receiver.toString());
		}
		intent.setAction(message);
		if (params != null) {
			intent.putExtras(params);
		}
		ACTIVITY.context.sendBroadcast(intent);
	}

	/**
	 * sendMessage 接收消息
	 * 
	 * @param message
	 *            :接收字段
	 * @param callback
	 *            :回调方法
	 */
	public static void receive(String message, Object receiver, CALLBACK<Bundle> callback) {
		List<CALLBACK<Bundle>> callbacks;
		if (receiver != null) {
			String category = receiver.toString();
			Map<String, List<CALLBACK<Bundle>>> allCallbacks;
			if (receiverCallbacks.containsKey(message)) {
				allCallbacks = receiverCallbacks.get(message);
			} else {
				allCallbacks = new HashMap<String, List<CALLBACK<Bundle>>>();
			}
			if (allCallbacks.containsKey(category)) {
				callbacks = allCallbacks.get(category);
			} else {
				callbacks = new ArrayList<CALLBACK<Bundle>>();
			}
			callbacks.add(callback);
			allCallbacks.put(category, callbacks);
			receiverCallbacks.put(message, allCallbacks);
		} else {
			if (globalCallbacks.containsKey(message)) {
				callbacks = globalCallbacks.get(message);
			} else {
				callbacks = new ArrayList<CALLBACK<Bundle>>();
			}
			callbacks.add(callback);
			globalCallbacks.put(message, callbacks);
		}
	}

}
